package com.antifly.common.dao.impl;

import com.antifly.common.dao.SysDictDao;
import com.antifly.common.dao.mapper.SysDictMapper;
import com.antifly.common.pojo.entity.SysDictDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 数据字典表(SysDict)Dao接口实现
 *
 * @author makejava
 * @date 2023-04-25
 */
@Component
public class SysDictDaoImpl extends ServiceImpl<SysDictMapper, SysDictDO> implements SysDictDao {

    @Autowired
    private SysDictMapper sysDictMapper;

    /**
     * 分页查询数据字典表信息
     *
     * @param key      搜索关键字
     * @param pageNum  当前页码
     * @param pageSize 每页展示数 当值为-1时默认展示全部
     * @return
     */
    @Override
    public Page<SysDictDO> selectSysDictList(String key, Integer pageNum, Integer pageSize) {
        Page<SysDictDO> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysDictDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(SysDictDO::getCreatedTime);
        return sysDictMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<SysDictDO> selectSysDictList() {
        LambdaQueryWrapper<SysDictDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(SysDictDO::getCreatedTime);
        return sysDictMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 查询所有数据字典表信息
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> selectMaps() {
        return sysDictMapper.selectMaps(null);
    }

    /**
     * 根据id查询数据字典表信息
     *
     * @param id 主键ID
     * @return
     */
    @Override
    public SysDictDO selectSysDictById(String id) {
        return sysDictMapper.selectById(id);
    }

    @Override
    public String insertSysDict(SysDictDO sysDictDO) {
        sysDictMapper.insert(sysDictDO);
        return sysDictDO.getId();
    }

    @Override
    public Boolean updateSysDict(SysDictDO sysDictDO) {
        int rows = sysDictMapper.updateById(sysDictDO);
        return rows > 0;
    }

    @Override
    public Boolean deleteSysDict(String sysDictId) {
        int rows = sysDictMapper.deleteById(sysDictId);
        return rows > 0;
    }

}